# arthor vuong.quoc
#! C:/Perl/bin/perl -w
use strict;
use warnings;
use diagnostics;
use Juniper;
#use autodie;
my @data;
# arr hold all router
my @arrRouter; 
sub readpara
{	
	my @fileName;
	#print "Input path : ";
	my $path = "D:/param";#<STDIN>;
	chomp($path);
	opendir (DIR, $path) or die $!;
	while(my $file = readdir(DIR))
	{
		if($file !~ m/^\./)
		{
			push(@fileName,$file);
		}
	}                                      
	closedir DIR;
	foreach my $file (@fileName)
	{
		my $topology;
		my $variable;
		my $testcase;
		my $paramsDefault;
		my $routerConfig;
		my @allRouter;
		my @connect;
		open File, ("+<$path"."/"."$file") or die $!;
		my @list = <File>;
		my $size = @list;
		my $i;
		my $flag = 0;
		my $row ="";
		for($i=0;$i<$size;$i++)
		{	
			
			# remove space & comment
			$list[$i] = &removeComment($list[$i]);
			# Read file to obj
			if($list[$i] =~ m/x-test-topology/)
			{
				while($list[$i] !~ m/\;/)
				{
					# $list[$i] = &removeComment($list[$i]);
					$topology = $topology."$list[$i]";
					$i++;
				}
				$topology = $topology."$list[$i]";
				
			}
			elsif($list[$i] =~ m/x-testcase-/)
			{
				while($list[$i] !~ m/\;/)
				{
					$list[$i] = &removeComment($list[$i]);
					$testcase = $testcase."$list[$i]";
					$i++;
				}
				$testcase = $testcase."$list[$i]";
				
			}
			elsif($list[$i] =~ m/x-/ && $list[$i] !~ m/x-testcase-/ && $list[$i] !~ m/x-test-topology/)	
			{
				while($list[$i] !~ m/\;/)
				{
					$list[$i] = &removeComment($list[$i]);
					$variable = $variable."$list[$i]";
					$i++;
				}
				$variable = $variable."$list[$i]";
			}elsif($list[$i] =~ m/params-default/)
			{
				# $paramsDefault = $paramsDefault."BEGINPram\n";	
				# print $list[$i];
				my $closep = 0 ;
				my $openp = 0;
				my $flagp = 0;
				while($flagp < 1 )
				{	
					$list[$i] = &removeComment($list[$i]);
					$paramsDefault = $paramsDefault.$list[$i];
					my @countopenp = $list[$i] =~ /\}/g;
					my @countclosep = $list[$i] =~ /\{/g;
					$openp = $openp + scalar @countopenp;
					$closep = $closep + scalar @countclosep;
					if($openp > 0 && $closep > 0 && $openp == $closep)
					{
						$flagp = 1 ;
						# $paramsDefault = $paramsDefault."\nENDPram\n";							
					}
					$i++;
				}	
				$i--;
			}	
			elsif($list[$i] =~ m/^r/ || $list[$i] =~ m/^h/)
			{
				# get router insert in arrRouter
				if($list[$i] =~ m/^r/)
				{
					my @arrs = split(' ',$list[$i]);
					# print $arrs[0],"\n";
					#push(@arrRouter, $arrs[0]);
					if($arrs[0] =~ m/{/)
					{
						$arrs[0] =~ s/{//;
					}
					push(@allRouter, $arrs[0]);
					&addRouter($arrs[0]);
				}
				# $routerConfig = $routerConfig."BEGIN\n";	
				# print $list[$i];
				my $close = 0 ;
				my $open = 0;
				my $flag = 0;
				while($flag < 1 )
				{	
					$list[$i] = &removeComment($list[$i]);
					# get connect
					if($list[$i] =~ m/^r/ && $list[$i] =~ m/-/ && $list[$i] !~ m/rou/ )
					{
						my $c = $list[$i];
						my @cn = split(' ',$c);
						# print $cn[0],"\n";
						push(@connect,$cn[0]);
					}
					elsif( $list[$i] =~ m/^link/)
					{
						my $c = $list[$i];
						my @cn = split(' ',$c);
						# print $cn[0],"\n";
						push(@connect,$cn[0]);
					}
					$routerConfig = $routerConfig.$list[$i];
					my @countopen = $list[$i] =~ /\}/g;
					my @countclose = $list[$i] =~ /\{/g;
					$open = $open + scalar @countopen;
					$close = $close + scalar @countclose;
					if($open > 0 && $close > 0 && $open == $close)
					{
						$flag = 1 ;
						$routerConfig = $routerConfig."\nendR\n";							
					}
					$i++;
				}	
				$i--;
			}			
		}
		# get data
		my $allR='';
		for my $r (@allRouter)
		{
			if($allR eq '')
			{
				$allR = $allR."$r";
			}else
			{
				$allR = $allR.";$r";
			}
		}
		my $con='';
		for my $c (@connect)
		{
			# print $c,"\n";
			if($con eq '')
			{
				$con = $con.$c;
			}else
			{
				$con = $con.";$c";
			}
		}
		my $object = new Juniper($file, $variable,$paramsDefault, $testcase, $topology, $routerConfig, $allR, $con);
		push(@data, $object);
		# Destroy
	}
	print "Size: ",scalar @data,"\n";
	# print $data[20]->showAll();
}
# show all data 
sub ShowAllData
{
	foreach my $file (@data)
	{
		print $file->getFileName(),"\n";
		# $file->getVariable();
		# print $file->getParamsDefault();
		# print $file->getTestCase();
		# print $file->getTopology();
		# print $file->getRouterConfig()
		# print $file->getAllRouter(),"\n";
		# my %topo = $file->allConnect();
		# print join "\t" ,keys %topo,"\n";
		# print join "\t" ,values %topo,"\n";
		my %hg = $file->getHashParamsDefault();
		print Dumper (\%hg);
	}
}

# Remove all Space in first and in the end.
sub regex_or {
  my $string = shift;
  $string =~ s/(?:^\s+)||(?: +$)//g;
  return $string;
}

sub regex_all {
  my $string = shift;
  $string =~ s/(?:^\s+)||(?:\s+$)//g;
  return $string;
}
sub for_plus {
	my $string = shift;
	for ($string) { s/^\s*//; s/\s*$//; }
	return $string;
}
sub double_star {
	my $string = shift;
	$string =~ s/^\s*//;
	$string =~ s/\s*$//;
	return $string;
}
# Remove all space and comment
sub removeComment
{
  my $string = shift;
  my @results = split("#",$string);
  $string =regex_or($results[0]);
  return $string;
}
# quoc.le
# Read all test case in a Object
# Return array obj Testcase
sub readTestCases()
{
	my @all_test_cases;  
	my @test_case;
	my $valTC = shift;
	my @valsTC = split(';',$valTC);
	for my $itemTC (@valsTC)
	{
		# print $itemTC,"\nEND\n";
		$itemTC = regex_or($itemTC);
		my $name = substr ($itemTC,0,15);
		if(length($itemTC) > 15)
		{
			$itemTC = substr ($itemTC,15);
			# print "Name : ",$name, "\n";
			# print "values : ",$itemTC, "\n";
			my $objectTC = new TestCases($name,$itemTC);
			push(@test_case,$objectTC);
			# print $objectTC->getNames(),"\n";
			# $objectTC->getValues();
		}
		
	}
	
	return @test_case;
}

# Add router if does not exist
sub addRouter
{
	my $router = shift;
	my $exist = 0;
	for my $r (@arrRouter)
	{
		if($r eq $router)
		{
			$exist = 1;
		}
	}
	if($exist == 0 )
	{
		push(@arrRouter,$router);
	}
}
# Show all router
sub ShowAllRouter
{
	# @arrRouter = sort @arrRouter;
	for my $itemR (sort @arrRouter)
	{
		print "$itemR ";
	}
	my $sizeR = @arrRouter;
	print "\nSize router : ", $sizeR;
}

sub makeTopo
{
	print "Make Topo\n";
	print "Misstake when connect have \" r0-1 \" , \"lone\", \"ingress\" \n";
	my %h1;
	foreach my $file (@data)
	{
		# print $file->getFileName(),"\n";
		my %h2 = $file->count_all_connect();# old version $file->allConnect();
		#print Dumper (\%h2);
		# print join "\t" ,keys %topo,"\n";
		# print join "\t" ,values %topo,"\n";
		
		foreach my $x ( keys %h2 ){
			if( exists $h1{ $x } ) 
			{
				$h1{ $x } = $h1{ $x } > $h2{ $x } ? $h1{ $x }:$h2{ $x };
			}
			else {
				$h1{ $x } = $h2{ $x };
			}
		}
	}
	# print Dumper sort(\%h1);
	print "\n \"rt-r0\" and \"link\" are the same!\n";
	return 	%h1;
}
sub printfile
{
	open F,"+>D:/all_connect.txt";
	foreach my $file (@data)
	{
		my %a = $file->router2hash();
		# if(exists %xxx)
		# {
			# my @hotgirl = keys %xxx;
			# for my $x (@hotgirl)
			# {
				# print F "$x\n";
			# }
		# }
		foreach my $k1 ( sort keys %a ) {
		  foreach my $k2 ( sort keys %{$a{$k1}} ) {
		    print F "  $k2";
		  }
		}
	print F "\n";	
	}
}
sub show_all_connect
{
	foreach my $file (@data)
	{
		print $file->getFileName(),"\n";
		my @aaa = $file->get_arr_connect_from_hash();
		print Dumper (\@aaa);
		my %aa = $file->count_all_connect();
		print Dumper (\%aa);
	}
}
sub show_all_connect_after_count
{
	foreach my $file (@data)
	{
		print $file->getFileName(),"\n";
		my %aa = $file->count_all_connect();
		print Dumper (\%aa);
	}
	
}
sub show_router_2_hash
{
	foreach my $file (@data)
	{
		# print $file->getFileName(),"\n";
		my %aa = $file->router2hash();
		print Dumper (\%aa);
	}
}
sub total_connect
{
	my %total = makeTopo();
	# print Dumper (\%total);
	my $sum = 0;
	for my $tt (values %total)
	{
		$sum += $tt;
	}
	return $sum;
}
#function reardpara()
&readpara();
# &ShowAllData();
# print $data[24]->getFileName(),"\n";
# print $data[2]->getTestCase();
# my %vl = $data[2]->getVariable();
# my %tt = $data[24]->getArrTestCase();
# my $var = join "\n", keys %tt;
# print $var;
# &ShowAllRouter();
# $data[2]->getDetailTestCases("x-testcase-001");
# my $stop = <STDIN>;

# print $data[2]->getArrTestCase();


# my @q = $data[2]->getArrTestCase();
# #print Dumper (\@q);
# for my $w (@q)
# {
	# print $w,"\n";	
# }

# &makeTopo();


#$data[4]->connectDetail();

# &makeTopo();
# $data[23]->change_link_to_connect();

# $data[31]->router2hash();

# my %xxx = $data[44]->router2hash();
# print Dumper (\%xxx);

# &printfile();

# my @aaa = $data[47]->get_arr_connect_from_hash();
# print Dumper (\@aaa);

# &show_all_connect_after_count();
# my %aa = $data[42]->count_all_connect();
# print Dumper (\%aa);

# &show_router_2_hash();
#&show_all_connect();
my %topo = &makeTopo();
print Dumper (\%topo);

print "All line : ",&total_connect(),"\n";